package sg.edu.nus.iss.ooad.jass.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import sg.edu.nus.iss.ooad.jass.dao.IJobseekerDAO;
import sg.edu.nus.iss.ooad.jass.dao.pattern.AbstractJASSDAOFactory;
import sg.edu.nus.iss.ooad.jass.entity.Jobseeker;

/**
 *
 * Jobseeker Controller. This class will handle all request related to the Jobseeker object.
 *
 * @author Isak Rabin (HT082193M)
 * @version 1.0
 * @since 1.0
 *private final static Logger logger
 * Creation / Modification History
 *         Isak Rabin        20-Sep-2009        Created
 *
 */
public class JobseekerController {

        private final static Logger logger = Logger.getLogger(JobseekerController.class.getName());
        private Jobseeker jobseeker = null;

        /**
         *
         */
        public JobseekerController() {
        }

        /**
         *
         * @return
         */
        public Jobseeker getJobseeker() {
                return jobseeker;
        }

        /**
         * 
         * @param jobseeker
         */
        public void setJobseeker(Jobseeker jobseeker) {
                this.jobseeker = jobseeker;
        }

        /**
         *
         * @param jobseeker
         * @return
         * @throws java.lang.Exception
         */
        public boolean createJobseeker(Jobseeker jobseeker) throws Exception {
                IJobseekerDAO dao = null;
                boolean retValue = false;

                try {
                        dao = AbstractJASSDAOFactory.getJASSDAO().getJobseekerDAO();
                        retValue = dao.createJobseeker(jobseeker);
                } catch (Exception ex) {
                        logger.log(Level.SEVERE, null, ex);
                        ex.printStackTrace();
                } finally {
                        dao = null;
                }

                return retValue;
        }

        /**
         *
         * @param id
         * @return
         * @throws java.lang.Exception
         */
        public Jobseeker retrieveJobseeker(String id) throws Exception {
                IJobseekerDAO dao = null;
                Jobseeker retValue = null;

                try {
                        dao = AbstractJASSDAOFactory.getJASSDAO().getJobseekerDAO();
                        retValue = dao.retrieveJobseeker(id);
                } catch (Exception ex) {
                        logger.log(Level.SEVERE, null, ex);
                        ex.printStackTrace();
                } finally {
                        dao = null;
                }

                return retValue;
        }

        /**
         *
         * @param jobseeker
         * @return
         * @throws java.lang.Exception
         */
        public boolean updateJobseeker(Jobseeker jobseeker) throws Exception {
                IJobseekerDAO dao = null;
                boolean retValue = false;

                try {
                        dao = AbstractJASSDAOFactory.getJASSDAO().getJobseekerDAO();
                        retValue = dao.updateJobseeker(jobseeker);
                } catch (Exception ex) {
                        logger.log(Level.SEVERE, null, ex);
                        ex.printStackTrace();
                        throw ex;
                } finally {
                        dao = null;
                }

                return retValue;
        }

        /**
         *
         * @param jobseeker
         * @return
         * @throws java.lang.Exception
         */
        public boolean deleteJobseeker(Jobseeker jobseeker) throws Exception {
                IJobseekerDAO dao = null;
                boolean retValue = false;

                try {
                        dao = AbstractJASSDAOFactory.getJASSDAO().getJobseekerDAO();
                        retValue = dao.deleteJobseeker(jobseeker);
                } catch (Exception ex) {
                        logger.log(Level.SEVERE, null, ex);
                        ex.printStackTrace();
                        throw ex;
                } finally {
                        dao = null;
                }

                return retValue;
        }

        /**
         *
         * @return
         * @throws java.lang.Exception
         */
        public List<Jobseeker> getAllJobseeker() throws Exception {

                IJobseekerDAO dao = null;
                List<Jobseeker> retValue = new ArrayList<Jobseeker>();

                try {
                        dao = AbstractJASSDAOFactory.getJASSDAO().getJobseekerDAO();
                        retValue = dao.getAllJobseeker();
                } catch (Exception ex) {
                        logger.log(Level.SEVERE, null, ex);
                        ex.printStackTrace();
                        throw ex;
                } finally {
                        dao = null;
                }

                return retValue;

        }

        /**
         *
         * @return
         * @throws java.lang.Exception
         */
        public Jobseeker getJobseekerByEmail(String email) throws Exception {

                IJobseekerDAO dao = null;
                Jobseeker retValue = new Jobseeker();

                try {
                        dao = AbstractJASSDAOFactory.getJASSDAO().getJobseekerDAO();
                        retValue = dao.getJobseekerByEmail(email);
                } catch (Exception ex) {
                        logger.log(Level.SEVERE, null, ex);
                        ex.printStackTrace();
                        throw ex;
                } finally {
                        dao = null;
                }
                return retValue;

        }

        /**
         * Load Jobseeker object from persistent and cache it.
         * @throws Exception
         */
        public synchronized static void cache() throws Exception {
                IJobseekerDAO dao = null;
                try {
                        dao = AbstractJASSDAOFactory.getJASSDAO().getJobseekerDAO();
                } catch (Exception ex) {
                        logger.log(Level.SEVERE, null, ex);
                        ex.printStackTrace();
                        throw ex;
                } finally {
                        dao = null;
                }
        }
}
